﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Drawing;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace ArmsCC
{
    class Helpers
    {
        public void Pulse()
        {
        }

        public void Cast(int spellId)
        {
            Cast(spellId, Color.LightBlue);
        }

        public void Cast(string spellName)
        {
            Cast(SpellManager.Spells[spellName].Id);
        }

        public void CastCooldown(int spellId)
        {
            Cast(spellId, Color.Yellow);
        }

        public void CastCooldown(string spellName)
        {
            CastCooldown(SpellManager.Spells[spellName].Id);
        }

        public void CastStance(int spellId)
        {
            Cast(spellId, Color.Green);
        }

        public void CastStance(string spellName)
        {
            CastStance(SpellManager.Spells[spellName].Id);
        }

        public bool CanCast(int spellId)
        {
            return SpellManager.HasSpell(spellId) && GetSpellCooldown(spellId) <= 0.25;
        }

        public bool CanCast(string spellName)
        {
            return CanCast(SpellManager.Spells[spellName].Id);
        }

        public bool IsAuraActive(WoWUnit targetName, int auraId)
        {
            return IsAuraActive(targetName, auraId, 0);
        }

        public bool IsAuraActive(WoWUnit targetName, int auraId, UInt64 creatorGUID)
        {
            return GetAuraTimeLeft(targetName, auraId, creatorGUID) != 0;
        }

        public bool IsAuraActive(WoWUnit targetName, string auraName)
        {
            return IsAuraActive(targetName, auraName, 0);
        }

        public bool IsAuraActive(WoWUnit targetName, string auraName, UInt64 creatorGUID)
        {
            return GetAuraTimeLeft(targetName, auraName, creatorGUID) != 0;
        }

        public double GetAuraTimeLeft(WoWUnit targetName, int auraId)
        {
            return GetAuraTimeLeft(targetName, auraId, 0);
        }

        public double GetAuraTimeLeft(WoWUnit targetName, int auraId, UInt64 creatorGUID)
        {
            WoWAuraCollection unitAuras = targetName.GetAllAuras();

            foreach (WoWAura curAura in unitAuras)
            {
                if (curAura.SpellId == auraId)
                {
                    if (creatorGUID == 0 || curAura.CreatorGuid == creatorGUID)
                    {
                        return curAura.TimeLeft.TotalSeconds;
                    }
                }
            }

            return 0;
        }

        public double GetAuraTimeLeft(WoWUnit targetName, string auraName)
        {
            return GetAuraTimeLeft(targetName, auraName, 0);
        }

        public double GetAuraTimeLeft(WoWUnit targetName, string auraName, UInt64 creatorGUID)
        {
            WoWAuraCollection unitAuras = targetName.GetAllAuras();

            foreach (WoWAura curAura in unitAuras)
            {
                if (curAura.Name == auraName)
                {
                    if (creatorGUID == 0 || curAura.CreatorGuid == creatorGUID)
                    {
                        return curAura.TimeLeft.TotalSeconds;
                    }
                }
            }

            return 0;
        }

        public double GetSpellCooldown(int spellId)
        {
            return GetSpellCooldown(GetSpellNameFromId(spellId));
        }

        public double GetSpellCooldown(string spellName)
        {
            return SpellManager.Spells[spellName].CooldownTimeLeft.TotalSeconds;
        }

        private void Cast(int spellId, Color color)
        {
            SpellManager.Cast(spellId);
            Logging.Write(color, "[" + ObjectManager.Me.CurrentRage + "] Casting " + GetSpellNameFromId(spellId));
        }

        private string GetSpellNameFromId(int spellId)
        {
            return WoWSpell.FromId(spellId).Name;
        }
    }
}
